<!-- 登录 -->
<template>
	<view class="page">
		<view class="logo">
			<image src="/static/logo.png" mode=""></image>
		</view>
		<!-- 填写区 -->
		<view class="input-info">
			<view class="info">
				<input type="tel" maxlength="11" v-model="form.phone" placeholder="手机号">
				<view class="more"></view>
			</view>
			<view class="info" :style="isLoginWay?'':'display: none'">
				<input type="tel" v-model="form.code" maxlength="6" placeholder="请输入验证码">
				<view class="more" @click="getCode">
					<text class="mo">{{!codeTime?'获取验证码':codeTime+'s'}}</text>
					<!-- 			<text class="mo">获取验证码</text>
					<text class="mo" style="display: none">59秒后重试</text> -->
				</view>
			</view>
			<view class="info" :style="isLoginWay?'display: none':''">
				<input :password='!isPassword' v-model="form.password" maxlength="26" placeholder="请输入密码">
				<view class="more">
					<text class="iconfont" :class="isPassword?'icon-eye-on':'icon-eye-off'"
						@click="isPassword = !isPassword"></text>
					<text class="mo">忘记密码</text>
				</view>
			</view>
		</view>
		<!-- 按钮 -->
		<view class="btn-info">
			<view class="btn" :style="isLogin?'opacity:1':'opacity:0.4'" @click="isLogin?onLogin():''">
				<text>登录</text>
			</view>
		</view>
		<!-- 操作 -->
		<view class="operation">
			<text @click="onLoginCut">{{isLoginWay?'手机号密码登录':'短信验证码登录'}}</text>
			<text @click="onRegister">还没有账号？立即注册</text>
		</view>

		<!-- 其他方式登录 -->
		<!-- 	<view class="other-ways">
			<text>其他登录方式</text>
		</view>
		<!-- 登录方式 -->
		<!-- <view class="login-way" @click="wechatLogin">
			<view class="way">
				<image src="/static/wx_ico.png" mode=""></image>
				<text>微信登录</text>
			</view>
		</view> -->
		<view>
			<!-- 提示信息弹窗 -->
			<uni-popup ref="message" type="message">
				<uni-popup-message :type="msgType" :message="messageText" :duration="1800"></uni-popup-message>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
	import {
		baseUrl
	} from "../../libs/request.js"
	export default {
		data() {
			return {
				codeTime: 0,
				messageText: '',
				msgType: '',
				isLogin: false,
				isLoginWay: false,
				isPassword: false,
				// 表单
				form: {
					phoneCode: '',
					rphone: '',
					phone: '',
					code: '',
					password: '',
					nickName: '',
					riderInfo: {},
				},
			};
		},
		methods: {
			onRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},
			/**
			 * 登录切换
			 */
			onLoginCut() {
				this.isLoginWay = !this.isLoginWay;
				// 验证码
				if (this.isLoginWay) {
					this.isLogin = this.form.code && this.form.phone ? true : false;
				}
				// 账号密码
				if (!this.isLoginWay) {
					this.isLogin = this.form.password && this.form.phone ? true : false;
				}
			},
			/**
			 * 登录点击
			 */
			onLogin() {
				/* 密码登录 */
				if (this.isLoginWay == false) {
					if (this.form.phone != "" && reg.test(this.form.phone)) {
						uni.request({
							url: baseUrl + "/rider/riderLogin",
							method: 'POST',
							data: {
								phone: this.form.phone,
								password: this.form.password,
							},
							header: {
								"Content-Type": "application/x-www-form-urlencoded"
							},
							success: (res) => {
								console.log("骑手密码登录信息：", res.data.riderInfo)
								if (res.data.riderInfo != null && res.data.riderInfo != "") {
									if (res.data.riderInfo.state == 23) {										
										uni.showToast({
											title: '你已被禁用！请联系管理员处理！',
											icon: 'none',
											duration: 2000
										});																
									} else {										
										uni.showToast({
											title: '登录成功！',
											icon: 'none',
											duration: 2000
										});	
										console.log(res.data.riderInfo)
										uni.setStorageSync("riderInfo", res.data.riderInfo)					
										setTimeout(function() {
											uni.switchTab({
												url: '../mine/mine',
											})
											}, 1000)
									}
								} else if (res.data.loginCode == 2) {									
									uni.showToast({
										title: '该手机号尚未注册！',
										icon: 'none',
										duration: 2000
									});	
								} else {									
									uni.showToast({
										title: '登录失败，密码错误！',
										icon: 'none',
										duration: 2000
									});	
								}

							}
						})
					} else {
						uni.showToast({
							title: '请正确输入手机号！',
							icon: 'none',
							duration: 2000
						});
					}
				}
				/* 验证码登录 */
				else {
					if (this.form.phone == this.form.rphone && this.form.code!=''&&reg.test(this.form.phone)) {
						uni.request({
							url:baseUrl+"/rider/codeLogin",
							method:"POST",
							data:{
								phone:this.form.phone,
								code:this.form.code,
							},
							header:{
								"Content-Type": "application/x-www-form-urlencoded"
							},
							success:(res)=>{
								if(res.data.riderInfo!=1&&res.data.riderInfo!=2){
									console.log("骑手登录信息：",res.data.riderInfo)
									
									uni.showToast({
										title: '登录成功！',
										icon: 'none',
										duration: 2000
									});	
									console.log(res.data.riderInfo)
									uni.setStorageSync("riderInfo", res.data.riderInfo)					
									setTimeout(function() {
										uni.switchTab({
											url: '../mine/mine',
										})
										}, 1000)																		
								}else if(res.data.riderInfo==1){
									uni.showToast({
										title: '验证码错误！',
										icon: 'none',
										duration: 2000
									});
								}else{
									uni.showToast({
										title: '验证码已失效！',
										icon: 'none',
										duration: 2000
									});
								}
							}
						})											
					} else if (this.form.phone != this.form.rphone) {
						uni.showToast({
							title: '请输入接收验证码的手机号！',
							icon: 'none',
							duration: 2000
						});
					} else {
						uni.showToast({
							title: '请认真检查完手机号和验证码再进行登录！',
							icon: 'none',
							duration: 2000
						});
					}
				}

			},
			/* 获取登录验证码 */
			getCode() {
				if (this.form.phone != "" && reg.test(this.form.phone)) {
					if (this.codeTime > 0) {
						uni.showToast({
							title: '不能重复获取',
							icon: "none"
						});
						return;
					} else {
						uni.request({
							url: baseUrl + "/rider/getRiderPhone",
							method: 'POST',
							data: {
								phone: this.form.phone
							},
							header: {
								"Content-Type": "application/x-www-form-urlencoded"
							},
							success: (res) => {
								if (res.data.phoneLogin == 2) {
									uni.showToast({
										title: '该手机号尚未注册！',
										icon: 'none',
										duration: 2000
									});
								} else if (res.data.phoneLogin = 1) {
									uni.showToast({
										title: '发送成功！',
										icon: 'none',
										duration: 2000
									});									
									this.codeTime = 60
									this.form.rphone = res.data.phone
									let timer = setInterval(() => {
										this.codeTime--;
										if (this.codeTime < 1) {
											clearInterval(timer);
											this.codeTime = 0
										}
									}, 1000)						
								}
							}
						})

					}
				} else {
					uni.showToast({
						title: '请正确输入手机号！',
						icon: 'none',
						duration: 2000
					});
				}
			},
			/* 
			 微信登录*/
			// wechatLogin() {
			// 	uni.showLoading({
			// 		title: '加载中',
			// 	});
			// 	uni.getUserProfile({
			// 		desc: '登录后可同步数据',
			// 		success: async (obj) => {
			// 			this.form.nickName = obj.userInfo.nickName
			// 			console.log('obj', obj.userInfo);
			// 			// 调用 action ，请求登录接口
			// 			await this.login(obj);
			// 			uni.login({
			// 				provider: 'weixin',
			// 				success: (res) => {
			// 					console.log('res-login', res);
			// 					this.code = res.code;
			// 					// console.log('code', res.code);
			// 					if (res.errMsg == 'login:ok') {
			// 						uni.setStorageSync('nickName', this.form.nickName);
			// 						uni.switchTab({
			// 							url: '../mine/mine',
			// 						})
			// 					}
			// 				},
			// 			});
			// 		},
			// 		fail: () => {
			// 			uni.showToast({
			// 				title: '授权已取消',
			// 				icon: 'error',
			// 				mask: true,
			// 			});
			// 		},
			// 		complete: () => {
			// 			// 隐藏loading
			// 			uni.hideLoading();
			// 		},
			// 	});
			// }
		},
		watch: {
			form: {
				handler(newValue, oldValue) {
					// 验证码
					if (this.isLoginWay) {
						this.isLogin = newValue.code && newValue.phone ? true : false;
					}
					// 账号密码
					if (!this.isLoginWay) {
						this.isLogin = newValue.password && newValue.phone ? true : false;
					}
				},
				deep: true
			}
		}
	}
</script>
<style scoped lang="scss">
	.page {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		background-color: #FFFFFF;
	}

	.logo {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 240rpx;

		image {
			width: 160rpx;
			height: 160rpx;
		}
	}

	/* 填写 */
	.input-info {
		padding: 0 6%;
		height: 240rpx;

		.info {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;
			height: 100rpx;
			border-bottom: 2rpx solid #f6f6f6;

			input {
				width: 70%;
				height: 100%;
				font-size: 26rpx;
				color: #222222;
			}

			.more {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				width: 30%;
				height: 100%;

				.iconfont {
					font-size: 34rpx;
				}

				.mo {
					font-size: 26rpx;
					padding-left: 20rpx;
					margin-left: 10rpx;
					border-left: 2rpx solid #EEEEEE;
				}
			}
		}
	}

	/* 按钮 */
	.btn-info {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100rpx;

		.btn {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90%;
			height: 80rpx;
			background: orange;
			border-radius: 100rpx;
			color: #FFFFFF;
			font-size: 28rpx;
			opacity: 0.4;
		}
	}

	/* 操作 */
	.operation {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 6%;
		height: 80rpx;

		text {
			font-size: 28rpx;
			color: #555555;
		}
	}

	/* 其他 */
	.other-ways {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		height: 80rpx;
		margin-top: 100rpx;

		text {
			font-size: 28rpx;
			color: #999999;
		}
	}

	.other-ways::after {
		content: "";
		width: 36%;
		height: 2rpx;
		background-color: #EEEEEE;
	}

	.other-ways::before {
		content: "";
		width: 36%;
		height: 2rpx;
		background-color: #EEEEEE;
	}

	/* 登录方式 */
	.login-way {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 200rpx;

		.way {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 50%;
			height: 100%;

			image {
				width: 100rpx;
				height: 100rpx;
			}

			text {
				font-size: 28rpx;
				color: #959595;
				margin-top: 20rpx;
			}
		}
	}
</style>
